#!/usr/local/bin/php -q
<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @co_author GameCharmer admin@gamecharmer.com - http://gaming.gamecharmer.com
* @package Age of Dragons Mod :: ( Game Console Required )
* @version $Id: cronjob.php 1.1.1 12-26-2009 Blackpawn
* @copyright (c) 2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
***************************** WARNING *****************************
* To ensure security of your games please take some of these steps 
* 1. Change the name of the crons folder to something else.
* 2. Change the name of this script to something else.
* 3. Choose a nice big password with all character types in it.
*******************************************************************
*/
echo '<br /><br />';

define('GC_MOD', true);
define('IN_PHPBB', true);
define('AOD', true);

$phpbb_root_path =  '../../../';
$templates_path = '../../../../games/aod/templates/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);
include($phpbb_root_path . 'games/aod/common.' . $phpEx);

$passwd = ($_GET['passwd']) ? $_GET['passwd'] : NULL;

if($passwd != $aod_config['cron_passwd'])
{
	die('Access Denied');
}

$gid = gc_fetch_gid('aod');
$games = gc_fetch_games();

$ticks_left = $games[$gid]['ticks_left'];
$game_time_mgt = $games[$gid]['time_mgt'];

$round_length = $games[$gid]['round_length'];
$round = $games[$gid]['round'];



if($ticks_left > 1)
{
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_gold = kingdom_gold +  ((kingdom_gold_mines * kingdom_gold_mines_income) * kingdom_gold_mines_lvl),
	kingdom_food = kingdom_food +  ((kingdom_farms * kingdom_farms_income) * kingdom_farms_lvl),
	kingdom_wood = kingdom_wood +  ((kingdom_timber_mills * kingdom_timber_mills_income) * kingdom_timber_mills_lvl),
	kingdom_iron = kingdom_iron +  ((kingdom_iron_mines * kingdom_iron_mines_income) * kingdom_iron_mines_lvl),
	kingdom_villagers = kingdom_villagers +  kingdom_villagers_income
	WHERE kingdom_status = 0");
	$db->sql_return_on_error(false);
	
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_gold = ((kingdom_vaults * kingdom_vaults_max) * kingdom_vaults_lvl)
	WHERE kingdom_gold > ((kingdom_vaults * kingdom_vaults_max) * kingdom_vaults_lvl)
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);
	
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_food = ((kingdom_granaries * kingdom_granaries_max) * kingdom_granaries_lvl)
	WHERE kingdom_food > ((kingdom_granaries * kingdom_granaries_max) * kingdom_granaries_lvl)
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);
	
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_wood = ((kingdom_woodsheds * kingdom_woodsheds_max) * kingdom_woodsheds_lvl)
	WHERE kingdom_wood > ((kingdom_woodsheds * kingdom_woodsheds_max) * kingdom_woodsheds_lvl)
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);
	
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_iron = ((kingdom_ironpits * kingdom_ironpits_max) * kingdom_ironpits_lvl)
	WHERE kingdom_iron > ((kingdom_ironpits * kingdom_ironpits_max) * kingdom_ironpits_lvl)
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);
	
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_turns = kingdom_turns + kingdom_turns_income
	WHERE kingdom_turns <= kingdom_turns_max 
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);
	
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_turns = kingdom_turns_max
	WHERE kingdom_turns > kingdom_turns_max
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);
	
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_stamina = kingdom_stamina + kingdom_stamina_income
	WHERE kingdom_stamina <= kingdom_stamina_max
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);
	
	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_stamina = kingdom_stamina_max
	WHERE kingdom_stamina > kingdom_stamina_max
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);

	$db->sql_query(
	"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
	kingdom_sponsor = kingdom_sponsor - 1
	WHERE kingdom_sponsor > 0
	AND kingdom_status = 0");
	$db->sql_return_on_error(false);

	$db->sql_query(
	"UPDATE " . GC_GAMES_TABLE ." SET 
	game_ticks_left = game_ticks_left - 1
	WHERE id = $gid");
	$db->sql_return_on_error(false);
	
	
}else{
	
	// Truncate kingdoms
	$sql = "TRUNCATE TABLE " . AOD_KINGDOMS_TABLE . "";
	$db->sql_query($sql);
	
	// Truncate kingdom news
	$sql = "TRUNCATE TABLE " . AOD_KINGDOM_NEWS_TABLE . "";
	$db->sql_query($sql);
	
	// Truncate reports
	$sql = "TRUNCATE TABLE " . AOD_REPORTS_TABLE . "";
	$db->sql_query($sql);

	// Remove Accounts From Game Console
	$sql = "DELETE FROM " . GC_ACCOUNTS_TABLE . " WHERE game_id = $gid";
	$db->sql_query($sql);



	// Record Networth Ranks
	$sql = "SELECT *
	FROM " . AOD_KINGDOMS_TABLE . "
	ORDER BY kingdom_net DESC";
	$result = $db->sql_query($sql);
				
	while ($row = $db->sql_fetchrow($result))
	{
		$uid = $row['user_id'];
		$name = $row['kingdom_name'];		
		$amount = $row['kingdom_net'];

		$sqlrank = 'INSERT INTO ' . AOD_NET_RANKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
			'user_id'	=> $uid,
			'kingdom_name' 	=> $name,
			'amount' 	=> $amount,
			'round' 	=> $round,
			)
		);		  
		$db->sql_query($sqlrank);
		
	}	
	$db->sql_freeresult($result);
	
	// Record Land Ranks
	$sql = "SELECT *
	FROM " . AOD_KINGDOMS_TABLE . "
	ORDER BY kingdom_total_land DESC";
	$result = $db->sql_query($sql);
				
	while ($row = $db->sql_fetchrow($result))
	{
		$uid = $row['user_id'];
		$name = $row['kingdom_name'];		
		$amount = $row['kingdom_total_land'];

		$sqlrank = 'INSERT INTO ' . AOD_LAND_RANKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
			'user_id'	=> $uid,
			'kingdom_name' 	=> $name,
			'amount' 	=> $amount,
			'round' 	=> $round,
			)
		);		  
		$db->sql_query($sqlrank);
		
	}	
	$db->sql_freeresult($result);
			
	// Record Population Ranks
	$sql = "SELECT *
	FROM " . AOD_KINGDOMS_TABLE . "
	ORDER BY kingdom_pop DESC";
	$result = $db->sql_query($sql);
				
	while ($row = $db->sql_fetchrow($result))
	{
		$uid = $row['user_id'];
		$name = $row['kingdom_name'];		
		$amount = $row['kingdom_pop'];

		$sqlrank = 'INSERT INTO ' . AOD_POP_RANKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
			'user_id'	=> $uid,
			'kingdom_name' 	=> $name,
			'amount' 	=> $amount,
			'round' 	=> $round,
			)
		);		  
		$db->sql_query($sqlrank);
		
	}	
	$db->sql_freeresult($result);	

	// Record Wins Ranks
	$sql = "SELECT *
	FROM " . AOD_KINGDOMS_TABLE . "
	ORDER BY kingdom_wins DESC";
	$result = $db->sql_query($sql);
				
	while ($row = $db->sql_fetchrow($result))
	{
		$uid = $row['user_id'];
		$name = $row['kingdom_name'];		
		$amount = $row['kingdom_wins'];

		$sqlrank = 'INSERT INTO ' . AOD_WINS_RANKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
			'user_id'	=> $uid,
			'kingdom_name' 	=> $name,
			'amount' 	=> $amount,
			'round' 	=> $round,
			)
		);		  
		$db->sql_query($sqlrank);
		
	}	
	$db->sql_freeresult($result);	

	// Reset the ticks left
	// 1 Hour / TimeMGT * 24 Hours * Days = New Ticks
	$new_ticks_left = ((floor(60 / $game_time_mgt) * 24) * $round_length);

	// Update game data in the game console game list
	$db->sql_query(
	"UPDATE " . GC_GAMES_TABLE ." SET 
	game_ticks_left = $new_ticks_left,
	game_round = game_round + 1,
	game_online = 2,
	game_open = 1
	WHERE id = $gid");
	$db->sql_return_on_error(false);	

			
}

echo 'Game ID : '.$gid.'<br>';
echo 'Round : '.$round.'<br>';
echo 'Tick Left : '.$ticks_left.'<br>';


?>